<script type="text/javascript">
	$(function(){
		$('.todelete').click(function(){
			var index = $(this).attr("data-index");
			var u = "<?php echo base_url()?>order/removeorder/";
			var d = {index:index};
			initCustom(u,d,function(data){
				if(data.flag){
					window.location.reload();
				}
			})
			return false;
		})
		$('#deleteOrder,#deleteOrderB').click(function(){
			$('#box1,#box1B').hide();
			$('#box2,#box2B').show();
		})
		$('#cancelBtn,#cancelBtnB').click(function(){
			$('#box2,#box2B').hide();
			$('#box1,#box1B').show();
		})
		$('#toDeleteOrder,#toDeleteOrderB').click(function(){
			var id = $(this).attr("value");
			var u = "<?php echo base_url()?>order/deleteOrder/";
			var d = {id:id};
			initCustom(u,d,function(data){
				if(data.flag){
					window.location.href = "<?php echo base_url() ?>"
				}
			})
			return false;
		})
	})
</script>

<?
if($order->OrderStatus!="Delete"){
?>
<div class="form-group" style="float:right">
	
	<p id="box1">
	<button id="deleteOrder" type="button" class="btn-waring">
		<i class="fa fa-trash-o"></i> ลบใบ Order นี้
	</button>
	</p>
	<p id="box2" style="display:none">
	<button id="toDeleteOrder" value="<?php echo $order->OrderID?>" type="button" class="btn-primary">
		<i class="fa fa-check"></i> ยืนยัน
	</button>
	<button id="cancelBtn" type="button" class="btn-default">
		 <i class="fa fa-times"></i> ยกเลิก
	</button>
	</p>
</div>
<?
}
?>
<?
include 'order_head.php';
?>
<?
if($order->OrderStatus!="Delete"){
?>
<h2 class="headSection">ค้นหาสินค้า</h2>
<div class="form-group">
	<label> รหัสสินค้า / หรือชือสินค้า </label>
	<input type="text" id="searchProduct" class="inputText normal-input" name="keyword" value="" />
	<button id="searchd" type="button" class="btn-search">
		<i class="fa fa-search"></i> ค้นหาสินค้า
	</button>
</div>
<? ?>
<form id="myForm" action="<?php echo $current?>updatecart" class="form-horizontal" role="form">
<div id="resultTable" class="none">
		<?php 
			echo $genhtml -> getHidden("OrderID", "OrderID");
			echo $genhtml -> getHidden("DocTypeID", "VatType");
		?>
		<button type="submit" class="btn-add">
			<i class="fa fa-shopping-cart"></i> Add To Cart
		</button>
		<div class="clear"></div>
		<br />
		<table class="myTable" style="width:100%">
			<tr>
				<th >รหัสสินค้า</th>
				<th >ชื่อสินค้า</th>
				<th >หน่วยขาย</th>
				<th >ราคาขาย</th>
				<th >คงเหลือ</th>
				
				<th class="qty">Carton</th>
				<th class="qty">Pack</th>
				<th class="qty">Ea</th>
				
			</tr>
			<tbody id="myBody">

			</tbody>
		</table>
		<div class="clear"></div>
		<br />
		<button type="submit" class="btn-add">
			<i class="fa fa-shopping-cart"></i> Add To Cart
		</button>
		
		

</div>

</form>
<br />

<?
if($this->cart->total_items() > 0){
?>
<h2 class="headSection">รายการสั่งซื้อปัจจุบัน</h2>
<table cellpadding="6" cellspacing="1"  border="0" class="myTable">
<tr>
	<th style="width:10%">ลบ</th>
  <th style="width:40%">สินค้า</th>
  <th style="text-align:right;width:10%">ราคาต่อหน่วย</th>
  <th style="text-align:center;width:10%">หน่วย</th>
  <th style="text-align:center;width:20%">จำนวน</th>
  
  
  <th style="text-align:right;width:10%">ราคารวม</th>
</tr>

<?php $i = 1; ?>

<?php foreach ($this->cart->contents() as $items): ?>

	<?
	
	$option = $this->cart->product_options($items['rowid']);
	
	$option = (array)$option;
	$pID = $option['MATNR'];
	$pName = $option['MAKTX'];
	$baseUnit = $option['BASE_UNIT'];
	if($baseUnit=="KAR"){
		$b = "ลัง";
	}else if($baseUnit=="PAK"){
		$b = "แพ็ก";
	}else {
		$b = "ชิ้น";
	}
	if(!isset($option['ISPremium'])){
	?>

	<tr>
		<td style="width:10%" align="center">
			<a data-index="<?php echo  $items['rowid']?>" href="#" class="todelete"><img style="width:20px;height:20px;" src="<?php echo base_url()?>asset/images/Delete-32.png" /></a>
			
		</td>
	  <td style="width:40%"><?php echo $pID ?><br />
		<?php echo $pName ?></td>
		 <td style="text-align:right;width:60px">
	  	<div style="width:60px;float:right;float:right;"><?php echo $this->cart->format_number($items['price']); ?></div>
	  </td>
		<td style="width:20%;text-align: center;"><?php echo $b ?></td>
		<td style="width:10%;text-align: right;"><?php echo converUnit($items,$option);?></td>
	 
	 
	  <td style="text-align:right;width:90px">
	  	<div style="width:90px;float:right;"><?php echo $this->cart->format_number($items['subtotal']); ?></div>
	  </td>
	</tr>

<?php $i++; }?>

<?php endforeach; ?>

<tr>
  <td colspan="4"> </td>
  <td class="right"><strong>ราคารวม</strong></td>
  <td class="right" style="text-align: right"><?php echo $this->cart->format_number($this->cart->total()); ?></td>
</tr>

</table>
<br />
<?
if($order->CreditRemain <= $this->cart->total()){
?>
	<div class="rowFrom rowWarning" style="padding:10px">
		<h2>วงเงินคงเหลือไม่มีเพียงพอสำหรับการสั่งซื้อ</h2>
		<p class="lead">กรอกรหัสอนุมัติการสั่งซื้อ</p>
		<p><label>Username</label> <input type="text" name="userApprove"></p>
		<p><label>Password</label> <input type="password" name="userApprovePassword"></p>
		<p><button type="submit" class="btn-primary" ><i class="fa fa-check-circle"></i> Submit</button>
		</p>
		<h3 style="margin-bottom:5px;">หรือ</h3>
		<p id="box1B">
	<button id="deleteOrderB" type="button">
		<i class="fa fa-trash-o"></i> ลบใบ Order นี้
	</button>
	</p>
	<p id="box2B" style="display:none">
	
	<button class="btn-default" id="cancelBtnB" type="button">
		<i class="fa fa-times"></i> กลับ
	</button>
	<button class="btn-primary" id="toDeleteOrderB" value="<?php echo $order->OrderID?>" type="button">
		<i class="fa fa-check"></i> ยืนยัน
	</button>
	</p>
		
	</div>
<?
}else{
	// if order over credit limit
?>
	<a href="<?php echo base_url()?>order/addpromotion/" class="btn-next "><i class="fa fa-chevron-right"></i> เลือกโปรโมชั่น</a>	
<?
}
}
}
?>


<script type="text/javascript">
	$(function(){
	
	$('#viewCart').click(function(){
		$('#myForm2').submit();
		
	})	
	initNormalForm(function(data){
			if(data.flag=="true"){
				alert("บันทึกข้อมูลเรียบร้อยแล้ว");
				window.location.reload();
			}
	})
	
	$('#searchd').click(function(){
		var pName = $('#searchProduct').val();
		initCustom('<?php echo $current?>seachproduct',
			{keyword:pName,'vatType':'<?php echo $order->VatType ?>'
				,'priceType':'<?php echo $order->PriceTypeID ?>'
			}
			,function(data){
				console.log(data);
				
			if(data.flag){
				var html = "";
				$.each(data.message,function(a,b){
					
					var stock  = b.STOCK;
					unit = "";
					var input1,input2,input3;
					

		var allQty = b.qty;
		//console.log("2");
		ratioEA = (b.EA_PACK / 1);
		if(b.EA_PACK!=0){
				
		
		
		ratioPack = (b.EA_CARTON / b.EA_PACK);
		currentCQty = Math.floor(allQty / b.EA_CARTON);
		
		
		if(currentCQty > 0 ){
			allQty = allQty - (currentCQty*b.EA_CARTON);
		}
		
		currentPQty = Math.floor(allQty / b.EA_PACK);;
		if(currentPQty > 0 ){
			allQty = allQty - (currentPQty*b.EA_PACK);
		}
		currentEQty = allQty;
		//console.log("5");
		
		if(b.BASE_UNIT=="EA"){
			unit = "ชิ้น";
			unit1 = '<input  style="width:100%" type="text" value="'+currentCQty+'" name="cQty[]" />';
			unit2 = '<select style="width:100%" name="pQty[]">';
			
			
			for(j=0;j<ratioPack;j++){
				select = (j==currentPQty)?' selected="selected"':"";
				unit2+='<option '+select+' value="'+j+'">'+j+'</option>';
			}
			unit2+='</select>'
			
			unit3 = '<select style="width:100%" name="eQty[]">';
			for(j=0;j<ratioEA;j++){
				select = (j==currentEQty)?' selected="selected"':"";
				unit3+='<option '+select+' value="'+j+'">'+j+'</option>';
			}
			unit3+='</select>'
		}else if(b.BASE_UNIT=="PAK"){
			unit = "แพ็ก";
			
			unit1 = '<input type="hidden" name="cQty[]" value="0" />';
			unit2 = '<input  style="width:40px" type="text" value="'+b.qty+'" name="pQty[]" />';
			unit3 = '<input  style="width:40px" type="text" value="'+b.qty+'" name="eQty[]" />';
		}else{
			unit = "ลัง";
			
			unit1 = '<input  style="width:40px" type="text" value="'+currentCQty+'" name="cQty[]" />';
			unit2 = '-<input type="hidden" name="pQty[]" value="0" />';
			unit3 = '-<input type="hidden" name="eQty[]" value="0" />';
			
		}
	html+='<tr>';
	html+='<td >'+b.MATNR+'</td>';
	html+='<td >'+b.MAKTX+'</td>';

			html+='<td align="right">'+unit+'</td>';
			
			html+='<td align="right">'+b.DisplayPrice+'</td>';
			html+='<td align="right">'+stock+'</td>';
	
			if(stock > 0 ){
				html+='<td class="qty"><input type="hidden" name="maxStock[]" value="'+stock+'" /><input type="hidden" name="price[]" value="'+b.CalPrice+'" />'+unit1+'</td>';
				html+='<td class="qty">'+unit2+'</td>';
				html+='<td class="qty">'+unit3+'<input type="hidden" name="qty[]" value="'+b.qty+'" /><input type="hidden" name="rowid[]" value="'+b.rowid+'" /><input type="hidden" name="pid[]" value="'+b.MATNR+'" /></td>';
			}else{
				html+='<td colspan="3">ไม่มีสินค้าใน Stock</td>';	
			}
	html+='</tr>'
	}
	})
	$('#myBody').html(html);
	$('#resultTable').show();
	}else{
	$('#resultTable').hide();
	$('#myBody').html("");
	alert(data.message);
	}
	});
	})
	
	})

</script>

